package net.jqsoft.demo.controller;


import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import net.jqsoft.demo.entity.Dormitory;
import net.jqsoft.demo.entity.Employee;
import net.jqsoft.demo.mapper.DormitoryMapper;
import net.jqsoft.demo.service.EmployeeService;

import lombok.extern.log4j.Log4j2;

@Controller
@RequestMapping("employee")
@Log4j2
public class EmployeeController extends ApiController{
	@Autowired
	private EmployeeService service;
	
	@Autowired
	private DormitoryMapper dormitoryMapper;
	
	@RequestMapping("list")
	public String list(Model model){
		Employee entity=new Employee();
		entity.setId(100);
		entity.setName("张三丰");
		entity.setAddress("安徽合肥");
		model.addAttribute("entity", entity);
		log.info("接口被调用了====");
		return "list";
	}
	@RequestMapping("get/{id}")
	@ResponseBody
	public Employee get(@PathVariable("id")Integer id){
		return service.getEntity(id);
	}
	@RequestMapping("updateDor/{id}/{code}")
	@ResponseBody
	public Object update(@PathVariable("id")Integer id,@PathVariable("code")String code){
		java.util.Random r=new java.util.Random(10); 
		Dormitory dormitory=new Dormitory();
		dormitory.setId(id);
		dormitory.setCode(code);
		dormitory.setDormName("修改后");
		return dormitoryMapper.update(dormitory);
	}
	@RequestMapping("updateEmp/{id}/{name}/{version}")
	@ResponseBody
	public Object updateEmployee(@PathVariable("id")Integer id,@PathVariable("name")String name,@PathVariable("version")Integer version){
		Employee entity=new Employee();
		entity.setName(name);
		entity.setAddress("安徽合肥");
		entity.setId(id);
		entity.setVersion(version);
		return service.updateById(entity);
	}
	@RequestMapping("add")
	@ResponseBody
	public Object insert(){
		Employee entity=new Employee();
		entity.setName("张三");
		entity.setAddress("安徽合肥");
		service.insert(entity);
		return entity;
	}
	@RequestMapping("page/{current}/{size}")
	@ResponseBody
	public IPage<Employee> pageSelect(@PathVariable("current")Integer current,@PathVariable("size")Integer size){
		IPage<Employee> page=new Page<>(current,size);
		return service.pageSelect(page);
	}
	
}
